<?php
/*
 * 授权书附件上传与删除接口
 */
require_once dirname(dirname(__FILE__)) . '/include/common.inc.php';
require_once INCLUDE_ROOT_PATH . 'file_server.class.php';
$c_id = isset($_REQUEST['c_id']) ? $_REQUEST['c_id'] : '';	//授权书ID
$act = isset($_REQUEST['act']) ? $_REQUEST['act'] : '';	//操作，upload上传，delete删除
$filename = isset($_REQUEST['filename']) ? $_REQUEST['filename'] : '';	//要操作的文件名
$token = isset($_REQUEST['token']) ? $_REQUEST['token'] : '';	//token验证
//|| ($act == 'upload' && empty($_FILES)) || ($act == 'delete' && empty($filename)) || ($act == 'view' && empty($filename))$token != md5($con_id . 'contractupload')
if (empty($c_id) || !is_numeric($c_id) || $c_id < 0 || ($act == 'upload' && empty($_FILES)) || ($act == 'delete' && empty($filename)) || ($act == 'view' && empty($filename)) || $token != md5($c_id . 'contract' . $act . $filename))
{
	$result = false;
}
else
{
	if(!file_exists(CREDIT_AUTHORIZATION_ROOT_PATH) && !is_dir(CREDIT_AUTHORIZATION_ROOT_PATH))
	{
		mkdir(CREDIT_AUTHORIZATION_ROOT_PATH);
	}

    $date_y = substr($filename,0,4);
    $date_m = substr($filename,4,2);
    $date_d = substr($filename,6,2);
    $filepath = CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/'. $date_m. '/'. $date_d. '/' . $c_id.'/';

	$File_server = new File_server();
	switch ($act)
	{
		case 'upload':
            if(!file_exists(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/') && !is_dir(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/'))
            {
                mkdir(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/');
            }
            if(!file_exists(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/'. $date_m. '/') && !is_dir(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/'. $date_m. '/'))
            {
                mkdir(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/'. $date_m. '/');
            }
            if(!file_exists(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/'. $date_m. '/'. $date_d. '/') && !is_dir(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/'. $date_m. '/'. $date_d. '/'))
            {
                mkdir(CREDIT_AUTHORIZATION_ROOT_PATH . $date_y. '/'. $date_m. '/'. $date_d. '/');
            }
            if(!file_exists($filepath) && !is_dir($filepath)){
                mkdir($filepath);
            }
			$result = $File_server->upload($filepath);
			break;

		case 'delete':
			$del_filepath = $filepath . $filename;
			if (is_file($del_filepath))
			{
				$result = $File_server->delete($del_filepath);
			}
			break;

		default:
			$result = false;
			break;
	}
}

echo json_encode($result);
?>